# encoding: utf-8
"""
@author: 夏洛
@QQ: 1972386194
@file: 01-demo.py
"""
import pyppeteer
import requests

print(pyppeteer.__chromium_revision__)

print(pyppeteer.executablePath())
from pyppeteer import launch
import asyncio
import time
async def main():
    # 启动一个浏览器  默认是无头
    browser = await launch(headless=False,args=['--disable-infobars','--window-size=1920,1080'])
    # 创建一个页面
    page = await browser.newPage()
    await page.setViewport({'width': 1200, 'height': 800})
    # 跳转到百度
    await page.goto("http://www.baidu.com/")
    # 输入要查询的关键字，type 第一个参数是元素的selector，第二个是要输入的关键字
    await page.type('#kw','pyppeteer')
    # 点击提交按钮 click 通过selector点击指定的元素
    await page.click('#su')
    time.sleep(3)
    await page.screenshot(path='example.png')
    await browser.close()

'''
外边的大小
页面展示大小
'''
# asyncio.get_event_loop().run_until_complete(main())

async def main1():
    # 启动一个浏览器  默认是无头
    browser = await launch(headless=False,args=['--disable-infobars','--window-size=1920,1080'])
    # 创建一个页面
    page = await browser.newPage()
    await page.setViewport({'width': 1200, 'height': 800})
    #  设置webdriver
    await page.evaluateOnNewDocument('() =>{ Object.defineProperties(navigator,'
                                     '{ webdriver:{ get: () => false } })}')

    await page.goto('https://www.zhipin.com/job_detail/?query=%E8%85%BE%E8%AE%AF%E7%88%AC%E8%99%AB&city=101020100&industry=&position=')
    dimensions = await page.evaluate('''() => {
            return {
                cookie: window.document.cookie,
            }
        }''')
    headers ={
        'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36',
        'Cookie': dimensions.get('cookie')
    }
    res = requests.get('https://www.zhipin.com/job_detail/?query=%E8%85%BE%E8%AE%AF%E7%88%AC%E8%99%AB&city=101020100&industry=&position='
                       ,headers=headers)
    print(res.text)



# asyncio.get_event_loop().run_until_complete(main1())


import asyncio
from pyppeteer import launch
from pyquery import PyQuery as pq
async def main2():
   browser = await launch(headless=False)    # 打开浏览器
   page = await browser.newPage()			# 开启选项卡
   # 输入地址访问页面
   await page.goto('https://careers.tencent.com/search.html?keyword=python')
   # 调用选折器
   await page.waitForXPath('//div[@class="recruit-wrap recruit-margin"]/div')
   # 获取网页源代码  带有数据的  绕过JS反爬虫
   doc = pq(await page.content())
   print(doc)
   # 提取数据
   title = [item.text() for item in doc('.recruit-title').items()]
   print('title:', title)
   # 关闭浏览器
   await browser.close()
# 启动异步方法
# asyncio.get_event_loop().run_until_complete(main2())



async def mains3():
    browser = await launch(headless=False, args=['--disable-infobars'])
    page = await browser.newPage()
    await page.setUserAgent("Mozilla/5.0 (Windows NT 6.0) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.36 Safari/536.5")
    await page.setViewport(viewport={'width': 1536, 'height': 768})
    await page.evaluateOnNewDocument('() =>{ Object.defineProperties(navigator,'
                                     '{ webdriver:{ get: () => false } }) }')
    await page.goto('https://pic.netbian.com/4kmeinv/index.html')
    elements = await page.querySelectorAll(".clearfix li a img")
    for item in elements:
        # 获取连接
        title_link = await (await item.getProperty('src')).jsonValue()
        print(title_link)
    await browser.close()
# asyncio.get_event_loop().run_until_complete(mains3())



async def mains4():
    browser = await launch(headless=True, args=['--disable-infobars'])
    page = await browser.newPage()
    await page.setUserAgent("Mozilla/5.0 (Windows NT 6.0) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.36 Safari/536.5")
    await page.setViewport(viewport={'width': 1536, 'height': 768})
    await page.evaluateOnNewDocument('() =>{ Object.defineProperties(navigator,'
                                     '{ webdriver:{ get: () => false } }) }')

    await page.goto("https://36kr.com/information/web_news/latest/")
    elements = await page.querySelectorAll(".title-wrapper a")

    for item in elements:
        title = await item.getProperty('textContent')
        titles  = await title.jsonValue()
        src =  await (await item.getProperty('href')).jsonValue()
        print(src, titles)

asyncio.get_event_loop().run_until_complete(mains4())








